/*
 *	ME2 C++ SDK
 *	Copyright (c) 2010 _silencer
 *
 *	Permission is hereby granted, free of charge, to any person
 *	obtaining a copy of this software and associated documentation
 *	files (the "Software"), to deal in the Software without
 *	restriction, including without limitation the rights to use,
 *	copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the
 *	Software is furnished to do so, subject to the following
 *	conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *	OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef UNREAL_FUNCTION_H
#define UNREAL_FUNCTION_H

#include "unreal_struct.h"

namespace unreal
{
	#pragma pack(push)
	#pragma pack(1)

	/* Member variables are not checked and could be incorrect! */
	struct uframe
	{	
		/*ustruct* node;
		uobject* object;
		uint8* code;
		uint8* locals;
		int32 line_num;
		uframe* previous_frame;*/
	};

	/*
		TODO write some dump code to check these flags from ut99 are still valid!
	 */
	enum ufunction_flags
	{
		ufunction_final			= 0x00000001,
		ufunction_defined		= 0x00000002,
		ufunction_iterator		= 0x00000004,
		ufunction_latent		= 0x00000008,
		ufunction_pre_operator	= 0x00000010,
		ufunction_singular		= 0x00000020,
		ufunction_net			= 0x00000040,
		ufunction_net_reliable	= 0x00000080,
		ufunction_simulated		= 0x00000100,
		ufunction_exec			= 0x00000200,
		ufunction_native		= 0x00000400,
		ufunction_event			= 0x00000800,
		ufunction_operator		= 0x00001000,
		ufunction_static		= 0x00002000,
		ufunction_no_export		= 0x00004000,
		ufunction_const			= 0x00008000,
		ufunction_invariant		= 0x00010000,

		ufunction_func_inherit			= ufunction_exec | ufunction_event,
		ufunction_func_override_match	= ufunction_exec | ufunction_final | ufunction_latent | ufunction_pre_operator | ufunction_iterator | ufunction_static,
		ufunction_net_func_flags		= ufunction_net | ufunction_net_reliable
	};

	typedef void (uobject::*ufunction_code_ptr)( uframe& stack, void* result );

	class ufunction: public ustruct
	{
		DECLARE_CORE_CLASS( ufunction, ustruct, Function )

	public:

		uint32 get_function_flags() const
		{
			return function_flags_;
		}

		uint16 get_native_index() const
		{
			return native_index_;
		}

		uname get_friendly_name() const
		{
			return friendly_name_;
		}

		uint8 get_number_of_params() const
		{
			return num_params_;
		}

		uint16 get_params_size() const
		{
			return params_size_;
		}

		uint16 get_return_value_offset() const
		{
			return return_value_offset_;
		}

		ufunction_code_ptr get_function_ptr()
		{
			return func_;
		}

		void set_function_flags( const uint32 flags )
		{
			function_flags_ = flags;
		}

		void set_native_index( const uint16 index )
		{
			native_index_ = index;
		}

		void set_friendly_name( const uname& name )
		{
			friendly_name_ = name;
		}

		void set_number_of_params( const uint8 number )
		{
			num_params_ = number;
		}

		void set_params_size( const uint16 size )
		{
			params_size_ = size;
		}

		void set_return_value_offset( const uint16 offset )
		{
			return_value_offset_ = offset;
		}

		void set_function_ptr( ufunction_code_ptr ptr )
		{
			func_ = ptr;
		}

	private:

		uint32 function_flags_;//correct
		uint16 native_index_;//correct
		uint16 rep_offset_;//rename to unknown?
		uname friendly_name_;//correct
		uint8 oper_precedence_;//rename to unknown?
		uint32 unknown0_;
		uint8 num_params_;//correct
		uint16 params_size_;//correct
		uint16 return_value_offset_;//correct
		uint32 unknown1_;
		uint16 unknown2_;
		ufunction_code_ptr func_;//correct
	};

	#pragma pack(pop)
}

#endif
